home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cqa.zip / PRTGRAP.TC < prev    next >
Text File  |  1991-04-01  |  3KB  |  122 lines

  1. QUESTION:  How does one go about printing graphics on a dot-matrix
  2.            printer?
  3.  
  4. ANSWER:    The following code is an example function which prints a
  5.            graphics screen on an Epson compatible printer.
  6.  
  7. PROTOTYPE: printimage(int left, int top, int right, int bottom);
  8.  
  9. -----------------------------------------------------------------
  10.  
  11. /*------------------------- PRTGRAP.C --------------------------*/
  12. #include <graphics.h>
  13. #include <stdio.h>
  14. #include <io.h>
  15.  
  16. #define        ESC       '\x1B'
  17. #define        LPT1      0
  18. #define        LPT2      1
  19.  
  20. #define        prn_putc(x)    biosprint(0,(x),LPT1)
  21.  
  22. /*-------------------------------------------------------------------
  23.   bitImage - Sets Epson printer to bit image mode. Nbytes is the
  24.              number of bytes to print.
  25. */
  26.  
  27. static void bitImage(int Nbytes)
  28. {
  29.   register int    n1, n2;
  30.  
  31.   n2 = Nbytes >> 8;
  32.   n1 = Nbytes - (n2 << 8);
  33.  
  34.   prn_putc(ESC);
  35.   prn_putc('*');
  36.   prn_putc(4);
  37.   prn_putc(n1);
  38.   prn_putc(n2);
  39. } /* end of bitImage() */
  40.  
  41. /*-------------------------------------------------------------------
  42.   getScrBits - Get pixels from the screen and convert them to
  43.                the printer's pin order.
  44. */
  45.  
  46. static unsigned char getScrBits(int x, int y)
  47. {
  48.   unsigned char firePins;
  49.  
  50.   firePins  = (getpixel(x, y++)==0)? 0: 0x80;
  51.   firePins |= (getpixel(x, y++)==0)? 0: 0x40;
  52.   firePins |= (getpixel(x, y++)==0)? 0: 0x20;
  53.   firePins |= (getpixel(x, y++)==0)? 0: 0x10;
  54.   firePins |= (getpixel(x, y++)==0)? 0: 0x08;
  55.   firePins |= (getpixel(x, y++)==0)? 0: 0x04;
  56.   firePins |= (getpixel(x, y++)==0)? 0: 0x02;
  57.   firePins |= (getpixel(x, y  )==0)? 0: 0x01;
  58.  
  59.   return firePins;
  60. } /* end of getScrBits() */
  61.  
  62. /*-------------------------------------------------------------------
  63.   printimage - Graphics print function.  */
  64.  
  65. */
  66.  
  67. int printimage(int left, int top, int right, int bottom)
  68. {
  69.   int x, y, width, height;
  70.  
  71.   width  = right-left;
  72.   height = bottom-top;
  73.  
  74.   /* Initialize line spacing to 7/72" */
  75.   prn_putc(ESC);
  76.   prn_putc('1');
  77.  
  78.   for (y=0; y<height; y+=8) {
  79.     bitImage(width);
  80.     for (x=0; x<width; x++) prn_putc(getScrBits(x,y));
  81.     prn_putc('\n');
  82.   }
  83.   return 0;
  84. } /* end of printimage() */
  85.  
  86. /****************************************************************
  87.   An example program which demonstrates a call to the above
  88.   function.
  89. */
  90.  
  91. main()
  92. {
  93.   int driver, mode, x, y;
  94.  
  95.   driver = DETECT; /* autodetect */
  96.   mode = 0;
  97.   initgraph(&driver, &mode, "");
  98.   x = getmaxx();
  99.   y = getmaxy();
  100.  
  101.   /* draw some things */
  102.   rectangle(0,0,x,y);
  103.   circle(300, 200, 100);
  104.   circle(210, 110, 50);
  105.   circle(390, 110, 50);
  106.   circle(270, 170, 10);
  107.   circle(272, 173, 3);
  108.   circle(330, 170, 10);
  109.   circle(332, 173, 3);
  110.   circle(300, 200, 10);
  111.   moveto(280, 220);
  112.   lineto(290, 230);
  113.   lineto(310, 230);
  114.   lineto(320, 220);
  115.  
  116.   /* Call the graphics print function. */
  117.   printimage(0, 0, x, y);  /* Print the entire screen. */
  118.  
  119.   closegraph();
  120.   return 0;
  121. }
  122.